<template>
	<view class="container">
		<lk-topbar :title="'我的日记'" @back="back"></lk-topbar>
		<view class="p20">
			<view class="timeTop">
				<view class="" @click="preDay">前一天</view>
				<picker mode="date" @change="pickerTap">
					<view class="">{{date}}<text class="c9 f28"
							v-if="date == $utils.getTime(new Date(),'Y年M月D日') ">当天</text> </view>
				</picker>
				<view class="" @click="nextDay">后一天</view>
			</view>
		</view>
		<view class="diaryBox">
			<view class="diaryItem" v-for="(item,index) in list" :key="index">
				<view class="close">
					<image @click="delectTap(item.type,item.id)"
						v-if="item.type == 'med' || item.type == 'sleep' || item.type == 'symptom' "
						src="../../static/images/del.png" mode=""></image>
				</view>
				<view class="time">{{item.time}}</view>
				<!-- 睡眠 back79ad6f 药物 backffb526 症状 back6aa1a8 自评backa171bd  监测 back38b781  筛查 backe47d5a  backa93c68e-->
				<!-- 药物 -->
				<template v-if="item.types =='med'">
					<view class="line backffb526"></view>
					<view class="content">
						<view class="" v-for="(it,ind) in item.med" :key='ind'>{{it.med_name}}{{it.spec}}{{it.dosage}}；
						</view>

					</view>
					<view class="icon arrow" @click="path('/plugins/prescription/index?type=detail&time='+item.time)">
					</view>
				</template>
				<!-- 监测 -->
				<template v-if="item.types=='quick_screen'">
					<view class="line backe47d5a"></view>
					<view class="content">
						<view class="">{{item.recommendation}}</view>
					</view>
					<view class="icon"></view>
				</template>
				<!-- 症状 -->
				<template v-if="item.types == 'symptom_monitor'">
					<view class="line back38b781"></view>
					<view class="content">
						<view class="">{{item.recommendation}}</view>
					</view>
					<view class="icon"></view>
				</template>
				<!-- 自查 -->
				<template v-if="item.types == 'self_assess'">
					<view class="line backa171bd"></view>
					<view class="content">
						<view class="mr20">{{item.score}}分</view>
						<view class="mr20">{{item.type == 'nm_edl'?'心理活动':'肢体活动'}}</view>
						<view class="">{{item.recommendation}}</view>
					</view>
					<view class="icon"></view>
				</template>
				<!-- 睡眠 -->
				<template v-if="item.types == 'sleep'">
					<view class="line back79ad6f"></view>
					<view class="content">
						<view class="mr20">{{item.moment == 0?'入睡':'醒来'}}</view>
						<view class="" v-for="(it,ind) in item.discomfort" :key='ind'>{{it}}；</view>
					</view>
					<view class="icon arrow" @click="path('/plugins/sleep/index?type=detail&time='+item.time)"></view>
				</template>
				<!-- 症状 -->
				<template v-if="item.types == 'symptom'">
					<view class="line back6aa1a8"></view>
					<view class="content">
						<view class="" v-for="(it,ind) in item.symptom" :key='ind'>
							{{it.where}}{{it.name}}；
						</view>
					</view>
					<view class="icon arrow" @click="path('/plugins/symptom/index?type=detail&time='+item.time)"></view>
				</template>
			</view>
		</view>


		<view class="btmBox">
			<view class="btmItem" @click="path('/plugins/prescription/index')">
				<image src="../../static/images/diary1.png" mode=""></image>
				<view class="">服药</view>
			</view>
			<view class="btmItem" @click="path('/plugins/symptom/index')">
				<image src="../../static/images/diary2.png" mode=""></image>
				<view class="">症状</view>
			</view>
			<view class="btmItem" @click="path('/plugins/sleep/index')">
				<image src="../../static/images/diary3.png" mode=""></image>
				<view class="">睡眠</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "",
		data() {
			return {
				days: "",
				date: '',
				isSave: true,
				allData: {
					med:[],
					quick_screen:[],
					self_assess:[],
					sleep:[],
					symptom:[],
					symptom_monitor:[]
				}
			}
		},
		onLoad() {
			this.date = this.$utils.getTime(new Date(), 'Y年M月D日');
			this.days = this.$utils.getTime(new Date(), 'Y-M-D');
		},
		onShow() {
			this.getData()
		},
		computed: {
			list() {
				let _this = this,
					newArr = [];
				if (JSON.stringify(_this.allData) != '{}') {
					_this.allData.self_assess.forEach((item, index) => {
							 item.time = item.time.substr(11,5)
						_this.$set(item, 'types', 'self_assess');
						_this.$set(item, 'id', index)
					})
					_this.allData.symptom.forEach((it, index) => {
						_this.$set(it, 'types', 'symptom');
						_this.$set(it, 'id', index);
						it.time = it.time.substr(0,5)
						it.symptom.forEach(item => {
							this.$switch.sid(item)
						})
					})
					_this.allData.med.forEach((item, index) => {
							 item.time = item.time.substr(0,5)
						_this.$set(item, 'types', 'med');
						_this.$set(item, 'id', index)
					})
					_this.allData.sleep.forEach((item, index) => {
							 item.time = item.time.substr(0,5)
						_this.$set(item, 'types', 'sleep');
						_this.$set(item, 'id', index)
					})
					_this.allData.quick_screen.forEach((item,index) => {
							 item.time = item.time.substr(0,5)
						_this.$set(item,'types','quick_screen');
						_this.$set(item,'id',index)
					})
					_this.allData.symptom_monitor.forEach((item,index) => {
						 item.time = item.time.substr(0,5)
						_this.$set(item,'types','symptom_monitor');
						_this.$set(item,'id',index)
					})

					newArr = [].concat(_this.allData.med, _this.allData.self_assess, _this.allData.sleep, _this.allData
						.symptom,	_this.allData.symptom_monitor,_this.allData.quick_screen);
					newArr.sort((a, b) => {
						return a.time < b.time ? -1 : 1
					})
				}
				
				return newArr;
			}
		},
		methods: {
			getData() {
				let _this = this;
				_this.$utils.request({
					url: "/patient/diary/",
					data: {
						date: _this.days
					}
				}).then(res => {
					_this.allData = res.data;
				})
			},
			delectTap(type, index) {
				let _this= this;
				uni.showModal({
					title:"提示",
					content:'确定删除',
					success(res) {
						if(res.confirm){			
							console.log(121311)
							_this.allData[type].splice(index, 1);
							console.log(121312)
							_this.$utils.request({
								url: "/patient/diary/",
								method: "POST",
								data: {
									date: _this.days,
									med: _this.allData.med,
									symptom: _this.allData.symptom,
									sleep: _this.allData.sleep,
								}
							}).then(res => {
								wx.showToast({
									title: "删除成功！"
								})
								_this.allData = res.data
							})
						}
					}
				})
				
			},
			// 前一天
			preDay() {
				let time = new Date(this.days).getTime() - 24 * 60 * 60 * 1000;
				this.date = this.$utils.getTime(time, 'Y年M月D日');
				this.days = this.$utils.getTime(time, 'Y-M-D');
				this.getData()
			},
			// 后一天
			nextDay() {
				let time = new Date(this.days).getTime() + 24 * 60 * 60 * 1000;
				this.date = this.$utils.getTime(time, 'Y年M月D日');
				this.days = this.$utils.getTime(time, 'Y-M-D');
				this.getData()
			},

			/**
			 * @param {Object} url 跳转路径
			 */
			path(url) {
				this.$routeJump.navigateTo(url)
			},
			//返回
			back() {
				this.$routeJump.back()
			},
			/**时间选择
			 * @param {Object} e 时间选择
			 */
			pickerTap(e) {
				this.date = this.$utils.getTime(e.detail.value, 'Y年M月D日');
				this.days = e.detail.value;
				this.getData()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.timeTop {
		height: 76rpx;
		line-height: 76rpx;
		@include flex(space-around, center)
	}

	.icon {
		width: 16px;
		height: 16px;
	}

	.diaryBox {
		width: 100%;
		max-height: calc(100vh - 450rpx);
		overflow-y: auto;
	}

	.diaryItem {
		@include flex(space-between, center);
		padding: 10rpx;

		.time {
			width: 120rpx;
			text-align: center;
		}

		.line {
			width: 20rpx;
			min-height: 70rpx;
		}

		.content {
			width: 480rpx;
			@include flex(flex-start, center);
			flex-wrap: wrap;
			font-size: 32rpx;
		}
	}

	.btmBox {
		width: 100%;
		height: 160rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
		position: fixed;
		bottom: 0;
		left: 0;
		background-color: #FFFFFF;

		.btmItem {
			image {
				width: 64rpx;
				height: 64rpx;
			}
		}
	}

	.close {
		width: 46rpx;
		height: 46rpx;
		margin-left: 14rpx;
	}
</style>
